package com.cbgolf.oa.views;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.cbgolf.oa.R;
import com.cbgolf.oa.adapter.recycle.CommonAdapter;
import com.cbgolf.oa.adapter.recycle.ViewHolder;
import com.cbgolf.oa.base.BaseViewNew;
import com.cbgolf.oa.contract.IMessageCenterContract.IMessageCenterView;
import com.cbgolf.oa.contract.IMessageCenterContract.IMessageCenterWaiter;
import com.cbgolf.oa.entity.MessageBean;
import com.cbgolf.oa.event.EventMsg;
import com.cbgolf.oa.manager.DataManager;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.DateUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.ScreenUtil;
import com.cbgolf.oa.util.TextUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import com.cbgolf.oa.widget.RecyclerItemDecoration;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lq
 */
public class MessageCenterViewImp extends BaseViewNew implements IMessageCenterView,
    OnRefreshLoadmoreListener {

  @BindView(R.id.topLayout_rl)
  View topLayout;
  @BindView(R.id.topBack_ll)
  View topBack;
  @BindView(R.id.title_tv)
  TextView titleTv;
  @BindView(R.id.refresh)
  SmartRefreshLayout refreshLayout;
  @BindView(R.id.recycleView)
  RecyclerView recyclerView;
  /**
   * worker
   */
  private IMessageCenterWaiter worker;

  /**
   * var
   *
   * @param act
   * @param waiter
   */
  private List<MessageBean> listData = new ArrayList<>();
  private CommonAdapter<MessageBean> adapter;

  public MessageCenterViewImp(Activity act, IMessageCenterWaiter waiter) {
    ButterKnife.bind(this, act);
    this.worker = waiter;
    super.init(act);
  }

  @Override
  protected void setViews() {
    ViewUtils.setText(titleTv, worker.getActivityTitle());
    ViewUtils.setRefreshTextHeader(context, refreshLayout);
    refreshLayout.setOnRefreshLoadmoreListener(this);
    setListAdapter();

  }

  @Override
  protected void setListener() {
    ViewUtils.setClick(topBack, v -> worker.back());
  }

  private void setListAdapter() {
    adapter = new CommonAdapter<MessageBean>(context, R.layout.item_rcy_message, listData) {
      @Override
      protected void convert(ViewHolder holder, MessageBean bean, int position) {
        if (null == bean) {
          return;
        }
        if (listData.size() == 1 && bean.isNoData) {
          holder.setVisible(R.id.layout, false)
              .setVisible(R.id.g_error_ll, true)
              .setText(R.id.g_error_tv, bean.errorMsg)
              .setImageResource(R.id.g_error_logo_iv, ErrorView.getLogoRes(bean.errorCode));
          ViewUtils.setLayoutParams(holder.getView(R.id.g_error_ll), ScreenUtil.getWidth(),
              ScreenUtil.getHeight() - ViewUtils.getTopAndStatusHeight(context, topLayout));
          return;
        }
        holder.setVisible(R.id.layout, true)
            .setVisible(R.id.g_error_ll, false)
            .setText(R.id.tv_item_msg_title, TextUtil.Text(bean.title))
            .setText(R.id.tv_item_msg_content, TextUtil.Text(bean.cotent))
            .setText(R.id.tv_item_msg_time, DateUtil.stampToDate(bean.createTime))
            .setImageResource(R.id.civ_item_msg, R.mipmap.logo_blue_round)
            .setVisible(R.id.tv_item_msg_redpoint, Util.isEquals(false, bean.read))
            .setOnClickListener(R.id.layout, v -> {
              if (Util.isEquals(false, bean.read)) {
                holder.setVisible(R.id.tv_item_msg_redpoint, false);
                worker.readMsg(bean.messageId);
              }
            });
      }
    };
    recyclerView.setHasFixedSize(true);
    LinearLayoutManager lm = new LinearLayoutManager(context);
    lm.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(lm);
    recyclerView.addItemDecoration(new RecyclerItemDecoration(1, 1, 0, 0));
    recyclerView.setAdapter(adapter);
  }

  @Override
  public void showProgress() {
    if (refreshLayout.isRefreshing() || refreshLayout.isLoading()) {
      return;
    }
    AnimaUtil.showLoading(context);
  }

  @Override
  public void stopProgress() {
    AnimaUtil.stopLoading();
    if (null != refreshLayout) {
      refreshLayout.finishLoadmore();
      refreshLayout.finishRefresh();
    }
  }

  @Override
  public void requestFail(String msg, int code) {
    listData.clear();
    listData.add(new MessageBean.Builder()
        .setIsNoData(true)
        .setError(msg, code)
        .build());
    adapter.notifyDataSetChanged();
  }

  @Override
  public void requestOver(EventMsg event) {
    if (null == event || event.target != DataManager.MESSAGE_ALL_LIST) {
      return;
    }
    stopProgress();
    switch (event.requestType) {
      case DataUtil.INITDATA:
        if (Util.listIsNull(event.listMsg)) {
          if (event.errorCode == ErrorUtil.NODATA_CODE) {
            requestFail(ErrorUtil.NOMESSAGE, ErrorUtil.MESSAGE_CODE);
            return;
          }
          requestFail(event.errorMsg, event.errorCode);
          return;
        }
        listData.clear();
        showData(event.listMsg);
        break;
      case DataUtil.REFRESH:
        if (Util.listIsNull(event.listMsg)) {
          return;
        }
        listData.clear();
        showData(event.listMsg);
        break;
      case DataUtil.LOADMORE:
        if (Util.listIsNull(event.listMsg)) {
          return;
        }
        showData(event.listMsg);
        break;
      default:
        break;
    }

  }

  private void showData(List<MessageBean> list) {
    listData.addAll(list);
    adapter.notifyDataSetChanged();
  }

  @Override
  public void onLoadmore(RefreshLayout refreshlayout) {
    worker.onLoadmore();
  }

  @Override
  public void onRefresh(RefreshLayout refreshlayout) {
    worker.onRefresh();
  }
}
